﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

namespace WebSite.admin.system
{
    public partial class navigatora_edit : BaseAdmin
    {


        // Methods
        private void Display(string parentid, string space)
        {
            string sql = "Select * From DB_Class Where parentid =" + parentid + " and languages=" + info.languages("") + " Order By classorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(space + "" + row["classname"].ToString() + "", "" + row["classid"].ToString() + ""));
                    this.Display(row["classid"].ToString(), "|--" + space);
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string str2;
            if (this.typeid.SelectedItem.Value.ToString() != "0")
            {
                str2 = this.typeid.SelectedItem.Value.ToString();
            }
            else
            {
                str2 = "0";
            }
            if (str2.ToString() != "0")
            {
                if (base.Request.QueryString["cid"] != null)
                {
                    string sql = "select top 1 * from DB_Class where classid=" + base.Request.QueryString["cid"];
                    DataTable table = new DataTable();
                    table = SQLAccess.GreatDs(sql).Tables[0];
                    if (table.Rows[0]["parentid"].ToString() != str2.ToString())
                    {
                        SQLAccess.DoSql("update DB_Class set amount=amount+1 where classid=" + str2.ToString() + "");
                        SQLAccess.DoSql("update DB_Class set amount=amount-1 where classid=" + table.Rows[0]["parentid"].ToString() + "");
                    }
                }
                else
                {
                    SQLAccess.DoSql("update DB_Class set amount=amount+1 where classid=" + str2.ToString() + "");
                }
            }
            if (base.Request.QueryString["cid"] != null)
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "update DB_Class set parentid=", str2.ToString(), ",classname='", re.replace(this.classname.Value), "',classorder=", re.replace(this.classorder.Value), ",visible=",  this.CheckBox1.Checked  ? "1" : "0", ",classlink='", re.replace(this.classlink.Value), "',system=",  this.CheckBox3.Checked  ? "1" : "0", ",classcontent='", this.WE_NewsContent.Text.Replace("'", "''"), "',ishead=",  this.CheckBox2.Checked  ? "1" : "0", 
                ",classpcs='", re.replace(this.classpcs.Value), "' where classid=", base.Request.QueryString["cid"], ""
             }));
                base.Response.Write("<script language='javascript'>alert('栏目修改成功');window.location.href='navigatora.aspx';</script>");
                base.Response.End();
            }
            else
            {
                SQLAccess.DoSql(string.Concat(new object[] { 
                "insert into DB_Class (parentid,classname,classorder,visible,classlink,system,classcontent,classpcs,ishead,languages) values (", str2.ToString(), ",'", re.replace(this.classname.Value), "',", re.replace(this.classorder.Value), ",",  this.CheckBox1.Checked  ? "1" : "0", ",'", re.replace(this.classlink.Value), "',",  this.CheckBox3.Checked  ? "1" : "0", ",'", this.WE_NewsContent.Text.Replace("'", "''"), "','其它',",  this.CheckBox2.Checked  ? "1" : "0", 
                ",", info.languages(""), ")"
             }));
                base.Response.Write("<script language='javascript'>alert('栏目添加成功');window.location.href='navigatora.aspx';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
       
            if(!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddMessage);
                this.Display("0", "|-");
                if (base.Request.QueryString["cid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            string sql = "select top 1 * from DB_Class where classid=" + base.Request.QueryString["cid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.typeid.SelectedValue = table.Rows[0]["parentid"].ToString();
            this.WE_NewsContent.Text = table.Rows[0]["classcontent"].ToString();
            this.classname.Value = table.Rows[0]["classname"].ToString();
            this.classorder.Value = table.Rows[0]["classorder"].ToString();
            this.classpcs.Value = table.Rows[0]["classpcs"].ToString();
            this.classlink.Value = table.Rows[0]["classlink"].ToString();
            this.CheckBox1.Checked = (bool)table.Rows[0]["visible"];
            this.CheckBox2.Checked = (bool)table.Rows[0]["ishead"];
            this.CheckBox3.Checked = (bool)table.Rows[0]["system"];
            if ((bool)table.Rows[0]["system"])
            {
                this.CheckBox3.Enabled = false;
            }
        }

        // Properties
        protected global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}